package com.zhangkunhao.notes.web.servlet;

import com.alibaba.fastjson.JSON;
import com.zhangkunhao.notes.entity.Do.Question;
import com.zhangkunhao.notes.entity.Do.Topic;
import com.zhangkunhao.notes.entity.Do.TopicPursueList;
import com.zhangkunhao.notes.entity.bo.Message;
import com.zhangkunhao.notes.entity.bo.PageBean;
import com.zhangkunhao.notes.service.TopicPursueListServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 话题关注列表的controller
 *
 * @author 20642
 */
@WebServlet("/topicPursueList/*")
public class TopicPursueListServlet extends BaseDaoServlet {

    private TopicPursueListServiceImpl topicPursueListService = new TopicPursueListServiceImpl();

    public void pursueTopic(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.接收数据
        String params = request.getReader().readLine();

        //2.转为对象
        Topic topic = JSON.parseObject(params, Topic.class);

        // 通过Session接收用户Id
        Integer userId = (Integer) request.getSession().getAttribute("userId");

        //3.调用service层,封装返回前端的对象
        Message<TopicPursueList> topicPursueListMessage = topicPursueListService.pursueTopic(userId, topic);

        //4.写数据,转为json
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(topicPursueListMessage));

    }

    public void cancelPursueTopic(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.接收数据
        String params = request.getReader().readLine();

        //2.转为对象
        Topic cancelTopic = JSON.parseObject(params, Topic.class);

        // 通过Session接收用户Id
        Integer userId = (Integer) request.getSession().getAttribute("userId");

        //2.调用service层,封装返回前端的对象
        Message<TopicPursueList> topicPursueListMessage = topicPursueListService.cancelPursueTopic(userId, cancelTopic);

        //3.写数据,转为json
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(topicPursueListMessage));
    }

    public void showMyPursueTopicQuestions(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //接收数据
        Integer userId = (Integer) request.getSession().getAttribute("userId");

        //调用service
        PageBean<Question> questionPageBean = topicPursueListService.showMyPursueTopicQuestions(userId);

        // 转换为json数据,写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(questionPageBean));
    }

    public void rankInterestedQuestionByLikes(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //接收数据
        Integer userId = (Integer) request.getSession().getAttribute("userId");

        //调用service
        PageBean<Question> questionPageBean = topicPursueListService.rankInterestedQuestionByLikes(userId);

        // 转换为json数据,写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(questionPageBean));
    }

    public void rankInterestedQuestionByLaunchTime(HttpServletRequest request, HttpServletResponse response) throws Exception {

        //接收数据
        Integer userId = (Integer) request.getSession().getAttribute("userId");

        //调用service
        PageBean<Question> questionPageBean = topicPursueListService.rankInterestedQuestionByLaunchTime(userId);

        // 转换为json数据,写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(questionPageBean));
    }

}
